<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
if(0){?><script><?php }?>
/**
 * UserForm 
 * SIP_NODE,SIP_NODE_ORG
 */
Ext.define('Hydcrm.user.UserForm', {
    extend: 'Ext.Panel',
    constructor: function(config) {
        config = config || {};
           
        config.border = false;
        config.autoScroll = true;
        config.items = this.buildForm(config.userid);
        if(config.userid > 0){
            config.bbar = [
                '->',
                {
                    text: '删除',
                    iconCls: 'icon-delete',
                    handler: function() {
                        this.onDelete(config.fd_id,config.fd_organizationid);
                    },
                    scope: this
                },
                {
                    text: '保存',
                    iconCls: 'icon-save',
                    handler: function() {
                        this.submitForm('edit_user',config.userid);
                    },
                    scope: this
                }
            ];
        }else{
            config.bbar = [
                '->',
                {
                    text: '保存',
                    iconCls: 'icon-save',
                    handler: function() {
                        this.submitForm('add_user');
                    },
                    scope: this
                }
            ];
        }
        
        this.addEvents({'savesuccess': true});
        if(config.userid){
            this.showForm(config.userid);
        }
        this.callParent([config]);
    },
    showForm: function(id) {  //重写show方法
        this.frmUser.load({
            url: Hydcrm.CONF.CONN_URL,
            params: {
                module: 'user',
                controller: 'user',
                action: 'load_user',
                id: id
            },
            waitMsg : '正在载入...',
            success: function(form, action) { //alert(action.result.data.provinceid);//alert(form.getValues().provinceid);  
                //Ext.getCmp('user_combo_cityid'+id).getStore().proxy.extraParams['provinceid'] = form.getValues().provinceid;     
                var cboUserCity = Ext.getCmp('user_combo_cityid'+id);
                var dsUserCity = Ext.getCmp('user_combo_cityid'+id).getStore();
                cboUserCity.enable();
                cboUserCity.reset();
                dsUserCity.proxy.extraParams['provinceid'] = form.getValues().provinceid;
                //alert(action.result.data.cityid);
                var onDsUserCityLoad = function() { 
                    cboUserCity.setValue(action.result.data.cityid);
                    dsUserCity.removeListener('load', onDsUserCityLoad, this);
                };
                dsUserCity.on('load', onDsUserCityLoad, this);
                dsUserCity.load();
            },
            failure: function() {
                Ext.Msg.alert('错误信息', '数据加载失败！');
            },
            scope: this
        });
    },
    buildForm: function(userid) {
        var dsUserProvince = Ext.create('Ext.data.Store', {
            fields: [ 'id','text'],
            proxy: {
                type: 'ajax',
                url: Hydcrm.CONF.CONN_URL,
                extraParams: {
                    module: 'team',
                    action: 'get_province_select'
                },
                reader: {
                    type: 'json',
                    root: Hydcrm.CONF.JSON_READER_ROOT
                }
            },
            autoLoad: true
        });
        var cboUserProvince = Ext.create('Ext.form.ComboBox', {
            fieldLabel: '所属省份',
            store: dsUserProvince,
            name: 'provinceid',
            queryMode: 'local',
            displayField: 'text',
            valueField: 'id',
            editable: false,
            allowBlank: false,
            padding: 6, 
            //labelStyle: 'color:red',
            msgTarget:'side',
            forceSelection: true,
            emptyText:'未选择',
            listeners: {
                select: onUserProvinceSelect,
                scope: this
            }
        });
        function onUserProvinceSelect(combo, value) {
            cboUserCity.enable();
            cboUserCity.reset();
            dsUserCity.proxy.extraParams['provinceid'] = value[0].get('id');
            dsUserCity.load();
        };
        var dsUserCity = Ext.create('Ext.data.Store', {
            fields: [ 'id','text'],
            proxy: {
                type: 'ajax',
                url: Hydcrm.CONF.CONN_URL,
                extraParams: {
                    module: 'team',
                    action: 'get_city_select',
                },
                reader: {
                    type: 'json',
                    root: Hydcrm.CONF.JSON_READER_ROOT
                }
            },
            autoLoad: true
        });
        var cboUserCity = Ext.create('Ext.form.ComboBox', {
            fieldLabel: '所属城市',
            store: dsUserCity,
            id: 'user_combo_cityid'+userid,
            name: 'cityid',
            queryMode: 'local',
            displayField: 'text',
            valueField: 'id',
            editable: false,
            allowBlank: false,
            padding: 6, 
            //labelStyle: 'color:red',
            msgTarget:'side',
            forceSelection: true,
            emptyText:'未选择',
            listeners: {
                select: onUserCitySelect,
                scope: this
            }
        });
        function onUserCitySelect(combo, value) {
            cboUserTeam.enable();
            cboUserTeam.reset();
            dsUserTeam.proxy.extraParams['cityid'] = value[0].get('id');
            dsUserTeam.load();
        };
        var dsUserTeam = Ext.create('Ext.data.Store', {
            fields: [ 'id','text'],
            proxy: {
                type: 'ajax',
                url: Hydcrm.CONF.CONN_URL,
                extraParams: {
                    module: 'team',
                    action: 'get_team_select',
                },
                reader: {
                    type: 'json',
                    root: Hydcrm.CONF.JSON_READER_ROOT
                }
            },
            autoLoad: true
        });
        var cboUserTeam = Ext.create('Ext.form.ComboBox', {
            fieldLabel: '团队名称',
            store: dsUserTeam,
            name: 'teamid',
            queryMode: 'local',
            displayField: 'text',
            valueField: 'id',
            editable: false,
            allowBlank: false,
            padding: 6, 
            //labelStyle: 'color:red',
            msgTarget:'side',
            forceSelection: true,
            emptyText:'未选择',
            listeners: {
                select: onUserTeamSelect,
                scope: this
            }
        });
        function onUserTeamSelect(combo, value) {
            cboUserDepartment.enable();
            cboUserDepartment.reset();
            dsUserDepartment.proxy.extraParams['teamid'] = value[0].get('id');
            dsUserDepartment.load();
        };
        var dsUserDepartment = Ext.create('Ext.data.Store', {
            fields: [ 'id','text'],
            proxy: {
                type: 'ajax',
                url: Hydcrm.CONF.CONN_URL,
                extraParams: {
                    module: 'user',
                    action: 'get_department_select'
                },
                reader: {
                    type: 'json',
                    root: Hydcrm.CONF.JSON_READER_ROOT
                }
            },
            autoLoad: true
        });
        var cboUserDepartment = Ext.create('Ext.form.ComboBox', {
            fieldLabel: '部门名称',
            store: dsUserDepartment,
            name: 'departmentid',
            queryMode: 'local',
            displayField: 'text',
            valueField: 'id',
            editable: false,
            allowBlank: false,
            padding: 6, 
            //labelStyle: 'color:red',
            msgTarget:'side',
            forceSelection: true,
            emptyText:'未选择'
        });
        var dsUserPosition = Ext.create('Ext.data.Store', {
            fields: [ 'id','text'],
            proxy: {
                type: 'ajax',
                url: Hydcrm.CONF.CONN_URL,
                extraParams: {
                    module: 'user',
                    action: 'get_position_select'
                },
                reader: {
                    type: 'json',
                    root: Hydcrm.CONF.JSON_READER_ROOT
                }
            },
            autoLoad: true
        });
        var cboUserPosition = Ext.create('Ext.form.ComboBox', {
            fieldLabel: '岗位名称',
            store: dsUserPosition,
            name: 'positionid',
            queryMode: 'local',
            displayField: 'text',
            valueField: 'id',
            editable: false,
            allowBlank: false,
            padding: 6, 
            //labelStyle: 'color:red',
            msgTarget:'side',
            forceSelection: true,
            emptyText:'未选择'
        });
        var cboUserLevel = Ext.create('Ext.form.ComboBox', {
            fieldLabel: '角色',
            store: [['0','员工'],['1','主管']],
            name: 'level',
            queryMode: 'local',
            displayField: 'text',
            valueField: 'id',
            editable: false,
            allowBlank: true,
            padding: 6, 
            //labelStyle: 'color:red',
            msgTarget:'side',
            forceSelection: true,
            emptyText:'员工'
        });
        this.frmUser = Ext.create('Ext.form.Panel', {
            border: 0,
            layout: 'column',columnWidth: .999,
            url: Hydcrm.CONF.CONN_URL,
            baseParams: {
                module: 'user'
            },
            bodyPadding: 10,
            fieldDefaults: {
                width: 380,
                labelAlign: 'left',
                labelWidth: 120,
            },
            defaults: {
                padding: 6,    
                listeners:{
                    render: function (field, p) {
                        if(field.name!='fd_options'){
                            //updateTip(field);
                        }
                    } 
                }
            },
            items: [//'id','fd_exten', 'fd_name', 'fd_userpin', 'fd_adminpin', 'fd_joinmsg','fd_options', 'fd_music','fd_recformat', 'fd_users'
                {xtype: 'hidden',name:'id',value:userid, readOnly:true },
                cboUserProvince,
                cboUserCity,
                cboUserTeam,
                cboUserDepartment,
                cboUserPosition,
                cboUserLevel,
                {xtype: 'textfield',name: 'name',readOnly: '',allowBlank:false,fieldLabel: '姓名'},
                {xtype: 'radiogroup',
                    fieldLabel: '性别',
                    defaults: {
                        name: 'sex'
                    },
                    items: [{
                        inputValue: 'M',
                        width: 100,
                        boxLabel: '女'
                    }, {
                        inputValue: 'F',
                        boxLabel: '男'
                    }]
                },
                {xtype: 'textfield',name: 'tel',readOnly: '',regex:/^[0-9]{0,12}$/, allowBlank:false, fieldLabel: '手机号'},
                {xtype: 'textfield',name: 'worknum',readOnly: '', allowBlank:true, fieldLabel: '工号'},
                {xtype: 'datefield',format: 'Y-m-d',value: new Date(),name: 'entrytime',fieldLabel: '入职时间'}
            ]
        });
        return this.frmUser;
    },
    submitForm: function(action,id) {
        this.frmUser.getForm().submit({
            url: Hydcrm.CONF.CONN_URL,
            params:{
                module: 'user',
                action: action,                             //'add_user','edit_user',
                id: id                              //edit
            }, 
            waitMsg: '正在提交，请稍等！',
            method: 'POST',
            success: function(form, action) {
                if(id>0){
                    Ext.getCmp('edit_window_user_'+id).close();
                }else{
                    Ext.getCmp('add_window_user').close();
                }
                list_user_ds.load();//Ext.Msg.alert(HydcrmLanguage.msgSuccessTitle,action.result.feedback);
            },
            failure: function(form, action) { //alert('失败');
                //Ext.Msg.alert(HydcrmLanguage.msgErrTitle, action.result.feedback);
            },
            scope: this
        });
    },
    onDelete: function(fd_id,fd_organizationid){
        Ext.MessageBox.confirm(
            'msgWarningTitle',
            'HydcrmLanguage',
            function(btn) {
                if (btn == 'yes') {
                    Ext.Ajax.request({
                        url: Hydcrm.CONF.CONN_URL,
                        params: {
                            module: 'user',
                            controller: 'user',
                            action: 'delete_user',
                            fd_id: fd_id
                        },
                        callback: function(options, success, response) {
                            var result = Ext.decode(response.responseText);
                            if (result.success == true) { //fireEvent('notifysuccess', result.feedback); //this.close();
                                //Ext.MessageBox.alert(HydcrmLanguage.msgSuccessTitle, result.feedback);
                                centerPanelUser.removeAll();
                                centerPanelUser.add(new Hydcrm.user.UserGrid({nodetype:'',fd_organizationid:fd_organizationid}));
                            } else {
                                Ext.MessageBox.alert(HydcrmLanguage.msgErrTitle, result.feedback);
                            }
                        },
                        scope: this
                    });
                }
            },
            this
        );
    }
});
